<?php

namespace Home\Controller;

use Home\Model\AccountsModel;

class AccountsController extends BaseController
{   protected $db;
    public function __init()
    {
        $this->db = new AccountsModel();
    }
    public function index(){
        if(!session('?username')){
            $this->error("请您先登录",u('Home/Login/index'));exit;
        }




//        dd($_SESSION);
        //获取get(商品的唯一标识)
        $gid=i('get.gid');

        $Conf=[
            'title'=>'结算页',
            'js'=>['account'],
        ];

        $this->assign('Conf',$Conf);
        $sgid=explode(',',rtrim($gid,','));

        //所有的seesion值
        $data=$_SESSION['cart'];
        if(!is_null($data))
        {
            foreach ($sgid as $v){
                //找到与标识相对应的数据
                $jiesData[] = $_SESSION['cart']['goods'][$v];
            }
            $this->assign('jiesData',$jiesData);
        }else{
            redirect(u('home/index/index'));
        }



 //*******************收货地址的展示***********************
       $accountsData =m('accounts')->select();
       $this->assign('accountsData',$accountsData);


//*************************收货地址的添加******************



//        dd($jiesData);


        $this->display();
    }


    public  function ajaxStore(){
        if (IS_POST){
            $data=i('post.');
            $aid=i('post.aid');
            if (empty($aid)) {
                $this->store($this->db, $data, u('index'));
             }else{
                $data['aid']=$aid;
                $this->store($this->db, $data, u('index'));
                }
            }
        }

        public function ajaxorder(){

        if (IS_AJAX){


           $data=$_POST;
           $data['time']=time();
           $data['uid']=session('uid');
           $data['number']=md5('wzq'.time());
           $oid=M('order')->add($data);
           if ($oid){
               $gid=explode(',',trim(i('get.gid'),','));
               foreach ($gid as $v){
                   $goodlist[]=[
                       'quantity'=>$_SESSION['cart']['goods'][$v]['num'],
                       'subtotal'=>$_SESSION['cart']['goods'][$v]['total'],
                       'glid'=>$_SESSION['cart']['goods'][$v]['glid'],
                       'glnumber'=>$_SESSION['cart']['goods'][$v]['number'],
                       'oid'=>$oid,
                       'gid'=>$_SESSION['cart']['goods'][$v]['id'],
                   ];
               }
               foreach ($goodlist as $v){
                   $res=m('order_list')->add($v);
               }

           }
           if($res){
               $this->ajaxReturn(['valid'=>1,'oid'=>$oid,'msg'=>'下单成功']);

           }
            $this->ajaxReturn(['valid'=>0,'msg'=>'下单失败']);

        }


        }



}